#!/bin/bash
###
 # @Description: 
 # @Features: 
 # @Author: jiangle
 # @Date: 2017-12-28 21:21:33
 # @LastEditTime: 2020-12-30 09:36:15
 # @LastEditors: jiangle
 # @Maintainer: jiangle
 # @Contributor: jiangle
### 

#linux
#hostip=`ifconfig wlp2s0 | grep "inet " | awk -F " " '{print $2}'`
#mac
hostip=`ifconfig en0 | grep "inet " | awk -F " " '{print $2}'`
cfgpath="$(dirname $(pwd))/config"
logpath="$(pwd)/log"
datapath="$(pwd)/data"

if [ "x$hostip" == "x" ]; then
    echo "cann't resolve host ip address"
    exit 1
fi

mkdir -p log
mkdir -p data

case "$1" in
zookeeper)
    # Create network for zookeeper containers
    zknet=`docker network ls | grep zknetwork | awk {'print $2'}`
    if [ -n "$zknet" ]
    then
        echo 'The zknetwork is already existed.'
    else
        echo 'Create zknetwork.'
        docker network create zknetwork
    fi
    ZOO_SERVERS="server.1=Codis-Z2181:2888:3888;2181 server.2=Codis-Z2182:2888:3888;2181 server.3=Codis-Z2183:2888:3888;2181"
    echo  ${ZOO_SERVERS}
    for ((i=0;i<3;i++)); do
        let port="2181 + i"
        let port1="2888 + i"
        let port2="3888 + i"
        let port3="8086 + i"
        let id="1 + i"
        mkdir -p ${datapath}/zookeeper_${port}
        docker rm -f      "Codis-Z${port}" &> /dev/null
        docker run -d --name "Codis-Z${port}" \
                --net zknetwork \
                -e ZOO_SERVERS="${ZOO_SERVERS}" \
                -e ZOO_MY_ID=${id} \
                -v ${datapath}/zookeeper_${port}:/data \
                -p ${port}:2181 \
                -p ${port1}:2888 \
                -p ${port2}:3888 \
                -p ${port3}:8080 \
                zookeeper:3.5.8
    done
    ;;

dashboard)
    docker rm -f      "Codis-D28080" &> /dev/null
    docker run --name "Codis-D28080" -d \
        --net zknetwork \
        --read-only -v ${cfgpath}/dashboard.toml:/codis/dashboard.toml \
                    -v ${logpath}:/codis/log \
        -p 28080:18080 \
        codis-image \
        codis-dashboard -l /codis/log/dashboard.log -c /codis/dashboard.toml --host-admin ${hostip}:28080
    ;;

proxy)
    docker rm -f      "Codis-P29000" &> /dev/null
    docker run --name "Codis-P29000" -d \
        --net zknetwork \
        --read-only -v ${cfgpath}/proxy.toml:/codis/proxy.toml \
                    -v ${logpath}:/codis/log \
        -p 29000:19000 -p 21080:11080 \
        codis-image \
        codis-proxy -l /codis/log/proxy.log -c /codis/proxy.toml --host-admin ${hostip}:21080 --host-proxy ${hostip}:29000
    ;;

server)
    for ((i=0;i<4;i++)); do
        let port="36379 + i"
        let c="1 + i"
        mkdir -p  ${datapath}/redis_${port}
        docker rm -f      "Codis-S${port}" &> /dev/null
        docker run --name "Codis-S${port}" -itd \
            --net host \
            -v ${cfgpath}/redis${c}.conf:/codis/redis.conf \
            -v ${logpath}:/codis/log \
            -v ${datapath}/redis_${port}:/codis/data \
            codis-image \
            codis-server /codis/redis.conf --logfile /codis/log/redis_${port}.log
    done
    ;;

sentinel)
    for ((i=0;i<3;i++)); do
        let port="46380 + i"
        docker rm -f      "Codis-T${port}" &> /dev/null
        docker run --name "Codis-T${port}" -d \
            -v ${cfgpath}/sentinel.conf:/codis/sentinel.conf \
            -v ${logpath}:/codis/log \
            -p ${port}:26379 \
            codis-image \
            redis-sentinel /codis/config/sentinel.conf --logfile /codis/log/sentinel_${port}.log --protected-mode no
    done
    ;;

fe)
    docker rm -f      "Codis-F8080" &> /dev/null
    docker run --name "Codis-F8080" -d \
         --net zknetwork \
         -v ${logpath}:/codis/log \
         -p 8080:8080 \
     codis-image \
     codis-fe -l /codis/log/fe.log --zookeeper Codis-Z2182:2181,Codis-Z2181:2181,Codis-Z2183:2181 --listen=0.0.0.0:8080 --assets=/codis/bin/assets
    ;;

redis)
    docker rm -f      "redis" &> /dev/null
    docker run --name "redis" -itd \
        -v /Users/shineedward/redis/redis.conf:/etc/redis/redis.conf \
        -v /Users/shineedward/redis/log:/tmp \
        -v /Users/shineedward/redis/data:/data \
        -p 6379:6379 \
        redis:latest \
        redis-server /etc/redis/redis.conf
    ;;

cleanup)
    docker rm -f      "Codis-D28080" &> /dev/null
    docker rm -f      "Codis-P29000" &> /dev/null
    docker rm -f      "Codis-F8080" &> /dev/null
    for ((i=0;i<4;i++)); do
        let port="36379 + i"
        docker rm -f      "Codis-S${port}" &> /dev/null
    done
    for ((i=0;i<3;i++)); do
        let port="2181 + i"
        docker rm -f      "Codis-Z${port}" &> /dev/null
    done
    for ((i=0;i<3;i++)); do
        let port="46380 + i"
        docker rm -f      "Codis-T${port}" &> /dev/null
    done
    rm -rf $logpath
    rm -rf $datapath
    ;;

*)
    echo "wrong argument(s)"
    ;;

esac
